<?php
include 'portlet_header.php';
if (getParameter("operation")!=null) $operation=getParameter("operation");
if (getParameter("portlet")!=null) $portlet=getParameter("portlet");
if (getParameter("portletId")!=null) $portletId=getParameter("portletId");
if (getParameter("pageId")!=null) $pageId=getParameter("pageId");

if (getParameter("portalId")!=null) $portalId=getParameter("portalId");

class SimpleXMLExtended extends SimpleXMLElement {
    public function addCData($cdata_text) {
        $node = dom_import_simplexml($this);
        $no   = $node->ownerDocument;
        $node->appendChild($no->createCDATASection($cdata_text));
    }
}

$LANG=strtoupper(getparameter("lang"));

if ($operation=="getservices")
{

    $contentName=getParameter("contentName");
$sSql ="SELECT * FROM portal_contentvalues INNER JOIN portal_content ON portal_content.id = portal_contentvalues.contentId WHERE portal_content.portalId = $portalId AND portal_content.`name` = '$contentName'";
 // echo $sSql ;
  $rs=$GLOBALS["conn"]->Execute($sSql);

    $content = new SimpleXMLExtended("<content/>");

    while (!$rs->EOF)  {

        $content->addChild("texte");
        $content->texte->addCData( $rs->fields["RichText_".$LANG."_value"]);

        $rs->MoveNext();
    }

    header('Content-type: text/xml');
echo $content->asXML();
}elseif ($operation=="getNews")
{


    $sSql ="SELECT
citybus_news.titre_$LANG as titre,
citybus_news.texte_$LANG as texte,
citybus_news.date,
citybus_news_images.image,
citybus_news.id
FROM
citybus_news
LEFT OUTER  JOIN citybus_news_images ON citybus_news_images.newsId = citybus_news.id
WHERE
citybus_news.portalId = $portalId
ORDER BY
citybus_news.date DESC";


    // echo $sSql ;
    $rs=$GLOBALS["conn"]->Execute($sSql);

    $xml = new SimpleXMLExtended("<datas/>");
$id=null;

    while (!$rs->EOF)  {

        if ($id==$rs->fields["id"]){
            $img= $images->addChild("img");
         //   $img->addAttribute("src",$rs->fields["image"]);
            $img->addAttribute("src","http://".getPortalHost().getFilesPath("news",false)."/".$rs->fields["image"]);
        }else
        {
            $data = $xml->addChild("data");
            $content= $data->addChild("content");
            $content->addAttribute("date",date('d.m.Y',strtotime($rs->fields["date"])));
            $content->addAttribute("titre",$rs->fields["titre"]);

            $content->addCData( $rs->fields["texte"]);
            $images= $data->addChild("images");
            if ($rs->fields["image"]!=""){
            $img= $images->addChild("img");

                $img->addAttribute("src","http://".getPortalHost().getFilesPath("news",false)."/".$rs->fields["image"]);
            }
        }

        $id=$rs->fields["id"];

      //  $content->data->addChild("texte");
//        $content->data->texte-
    //    $content->data->texte->addCData( $rs->fields["texte"]);

        $rs->MoveNext();
    }
    header('Content-type: text/xml');
    echo $xml->asXML();

}
    elseif ($operation=="getparcautobus")
{


    $sSql ="SELECT
citybus_parcautobus.image
FROM
citybus_parcautobus
WHERE
citybus_parcautobus.portalId = $portalId
ORDER BY
citybus_parcautobus.id DESC";


    // echo $sSql ;
    $rs=$GLOBALS["conn"]->Execute($sSql);

    $xml = new SimpleXMLExtended("<images/>");
    $id=null;

    while (!$rs->EOF)  {




                $img= $xml->addChild("image");
                $img->addAttribute("path","http://".getPortalHost().getFilesPath("parcautobus",false)."/".$rs->fields["image"]);




        //  $content->data->addChild("texte");
//        $content->data->texte-
        //    $content->data->texte->addCData( $rs->fields["texte"]);

        $rs->MoveNext();

    }
    header('Content-type: text/xml');
    echo $xml->asXML();


} elseif ($operation=="getbackgrounds")
{


    $sSql ="SELECT
citybus_backgrounds.image
FROM
citybus_backgrounds
WHERE
citybus_backgrounds.portalId = $portalId
ORDER BY
citybus_backgrounds.id DESC";


    // echo $sSql ;
    $rs=$GLOBALS["conn"]->Execute($sSql);

    $xml = new SimpleXMLExtended("<images/>");
    $id=null;

    while (!$rs->EOF)  {




        $img= $xml->addChild("image");
        $img->addAttribute("path","http://".getPortalHost().getFilesPath("backgrounds",false)."/".$rs->fields["image"]);




        //  $content->data->addChild("texte");
//        $content->data->texte-
        //    $content->data->texte->addCData( $rs->fields["texte"]);

        $rs->MoveNext();

    }


    header('Content-type: text/xml');
    echo $xml->asXML();
} elseif ($operation=="getLignes")
{


    $sSql ="SELECT
*
FROM
citybus_lignes
WHERE
citybus_lignes.portalId = $portalId
ORDER BY
citybus_lignes.numero ASC";


    // echo $sSql ;
    $rs=$GLOBALS["conn"]->Execute($sSql);

    $xml = new SimpleXMLExtended("<lignes/>");
    $id=null;

    while (!$rs->EOF)  {




        $ligne= $xml->addChild("ligne");
        $ligne->addAttribute("numero",$rs->fields["numero"]);
        $ligne->addAttribute("itineraire",$rs->fields["itineraire_$LANG"]);
        $allers= $ligne->addChild("aller");


        $array = explode("\n",$rs->fields["aller_$LANG"]);

        foreach($array as $station) {
            $station=str_replace("\r","",$station);

        $aller= $allers->addChild("station");

        $aller->addAttribute("value",$station);
        }



        $retours= $ligne->addChild("retour");


        $array = preg_split("#\n#",$rs->fields["retour_$LANG"]);

        foreach($array as $station) {
            $station=str_replace("\r","",$station);
            $retour= $retours->addChild("station");

            $retour->addAttribute("value",$station);
        }


      //  $ligne->addAttribute("path","http://".getPortalHost().getFilesPath("parcautobus",false)."/".$rs->fields["image"]);




        //  $content->data->addChild("texte");
//        $content->data->texte-
        //    $content->data->texte->addCData( $rs->fields["texte"]);

        $rs->MoveNext();

    }
    header('Content-type: text/xml');
    echo $xml->asXML();


}elseif ($operation=="getContent")
{
    $contentName=getParameter("contentName");

    $sSql ="SELECT portal_contentvalues.TextField_".$LANG."_value as TextField_value,
portal_contentvalues.TextArea_".$LANG."_value as TextArea_value,
portal_contentvalues.RichText_".$LANG."_value as RichText_value,
portal_contentvalues.Datevalue,
portal_contentvalues.InternalLinkvalue,
portal_contentvalues.Flashvalue,
portal_contentvalues.FlashWidthvalue,
portal_contentvalues.FlashHeightvalue,
portal_contentvalues.FlashVarsvalue,
portal_contentvalues.Imagevalue,
portal_contentvalues.Imagevalue_ext,
portal_contentvalues.Image_arvalue_ext,
portal_contentvalues.Image_arvalue,
portal_contentvalues.Queryvalue,
portal_contentvalues.Filevalue,
portal_contentvalues.YoutubeVideovalue,
portal_contentvalues.YoutubeVideoheightvalue,
portal_contentvalues.YoutubeVideowidthvalue,
portal_contentvalues.Map_Longvalue,
portal_contentvalues.Map_Latvalue,
portal_contentvalues.authoringelementId,
portal_contentvalues.portalId,
portal_authoringtemplate_elements.`name`,
portal_authoringelement.`name` as type
FROM
portal_contentvalues
INNER JOIN portal_content ON portal_content.id = portal_contentvalues.contentId
INNER JOIN portal_authoringtemplate_elements ON portal_contentvalues.authoringtemplate_elementId = portal_authoringtemplate_elements.id
INNER JOIN portal_authoringelement ON portal_authoringtemplate_elements.elementId = portal_authoringelement.id
WHERE portal_content.portalId = $portalId AND portal_content.`name` = '$contentName'";


     //echo $sSql ;
    $rs=$GLOBALS["conn"]->Execute($sSql);

    $content = new SimpleXMLExtended("<content/>");


    while (!$rs->EOF)  {



        $name=$rs->fields["name"];
        $$name= $content->addChild($name);

        $content->$name->addCData( $rs->fields[$rs->fields["type"]."_value"]);

        $rs->MoveNext();

    }
    header('Content-type: text/xml');
    echo $content->asXML();


}
include 'portlet_footer.php';
?>